--[[
 -- *名称:RecordPart
 -- *描述:牌局记录
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-10-16
 -- *修改日期:2017-10-16
 ]]
local CURRENT_MODULE_NAME = ...
local RecordPart = class("RecordPart", PokerPartBase) --登录模块
RecordPart.DEFAULT_PART = {}
RecordPart.DEFAULT_VIEW = "recordlayer"

---
-- [ctor 默认part构造方法]
-- @DateTime 2017-09-20
-- @param    owner [description]
-- @param    data [description]
-- @return    [description]
--
function RecordPart:ctor(owner, data)
    RecordPart.super.ctor(self, owner, data)
    self._data = data;
end


---
-- [activate 默认激活该part调用方法]
-- @DateTime 2017-09-20
-- @param    data [description]
-- @return    [description]
--
function RecordPart:activate(data)
    RecordPart.super.activate(self, CURRENT_MODULE_NAME)
    cc.bind(self, "loadinglogic");

    self._reqHistoryEntryId = FrameUtils.addEnterFrameListener(function (  )
    	self:reqGameHistory();
    	FrameUtils.removeEnterFrameListener(self._reqHistoryEntryId);
    	self._reqHistoryEntryId = nil;
    end, 0.4);

    -- self:test();
end

---
-- [deactivate 销毁该part调用方法]
-- @DateTime 2017-09-20
-- @return   [description]
--
function RecordPart:deactivate()
    RecordPart.super.deactivate(self, CURRENT_MODULE_NAME)
    if self.view == nil then
        return
    else
        self.view:removeSelf()
        self.view = nil
    end
end

---
-- [getPartId 获取该part的partId]
-- @DateTime 2017-09-20
-- @return   [description]
--
function RecordPart:getPartId()
    return "RecordPart"
end

function RecordPart:hideView()
end

function RecordPart:showView()

end

function RecordPart:reqGameHistory()
    local reqData = ddz_pb.RoomScoreRecordListReq();
    local game_user = global:getGameUser();
    reqData.playerIndex = tonumber(game_user:getProp("gameplayer" .. DdzGameConfig.GAME_ID).playerIndex);
    reqData.dayNum = 2;
    reqData.listNum = 30;
    global:getNetManager():sendProtoMsg(reqData, DdzSocketConfig.REQ_GAME_HISTORY, DdzGameConfig.GAME_ID);
end

function RecordPart:_onGetGameHistory(data)
    Log.d("RecordPart:_onGetGameHistory");
    local history = {};
    for k, v in ipairs(data.recordList) do 
    	local extendInfo = v.Extensions[ddz_pb.DDZRoomRecordPoker.extend_data];
    	local record = {};
    	record.players = {};
    	record.roomId = v.room_id;
    	record.tableIndex = v.table_index;
    	record.pos = v.pos;
    	record.startTime = v.start_time;
    	record.endTime = v.end_time;
    	record.hostId = v.host_id;
    	record.hostName = v.host_name;
    	record.roomType = v.room_type;
    	record.payType = v.payType;
    	record.handNum = v.handNum;
    	record.totalHandNum = v.totalHandNum;

    	local p1 = {};	-- 玩家1
    	p1.playerId = v.player1_id;
    	p1.playerName = v.player1_name;
    	p1.score = v.score1;
    	p1.headImgUrl = v.player1_img_url;
    	table.insert(record.players, p1);

    	local p2 = {};	-- 玩家2
    	p2.playerId = extendInfo.player2_id;
    	p2.playerName = extendInfo.player2_name;
    	p2.score = extendInfo.score2;
    	p2.headImgUrl = extendInfo.player2_img_url;
    	table.insert(record.players, p2);

    	local p3 = {};	-- 玩家3
    	p3.playerId = extendInfo.player3_id;
    	p3.playerName = extendInfo.player3_name;
    	p3.score = extendInfo.score3;
    	p3.headImgUrl = extendInfo.player3_img_url;
    	table.insert(record.players, p3);

    	table.insert(history, record);
    end
    Log.d("RecordPart:_onGetGameHistory history: ", history);
    self:updateView("refreshView", history);
end

function RecordPart:test(  )
	local history = {};
	for i = 1, 10 do 
		local record = {};
    	record.players = {};
    	record.roomId = "v.room_id";
    	record.tableIndex = math.random(100000, 999999);
    	record.pos = 1;
    	record.startTime = "2017-11-10 15:00:00";
    	record.endTime = "2017-11-10 15:00:00";
    	record.hostId = math.random(1, 3);
    	record.hostName = "v.host_name";
    	record.roomType = "v.room_type";
    	record.payType = 0;
    	record.handNum = math.random(1, 16);
    	record.totalHandNum = 16;

    	for j = 1, 3 do 
    		local p1 = {};
	    	p1.playerId = j;
	    	p1.playerName = "雷神不怕雷" .. j;
	    	p1.score = math.random(-100, 100);
    		p1.headImgUrl = "http://v1.qzone.cc/avatar/201710/09/18/00/59db48d19af7d457.jpeg!180x180.jpg";
	    	table.insert(record.players, p1);
    	end
		table.insert(history, record);
	end
    self:updateView("refreshView", history);
end

RecordPart.eventFuncMap = {
    [DdzEventConfig.REC_GAME_HISTORY] = RecordPart._onGetGameHistory;
};

return RecordPart 